﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using WebApplication1.classes.Core.Chrome.Bookmarks.Model;

namespace WebApplication1.classes.Core.Chrome.Bookmarks.Wrapper {
    public class Bookmark : ChromeBookmark {
        private Bookmark parent_;
        private BookmarkRoots roots_;
        private string serviceNameForOpen_;

        public Bookmark() {
            // TODO: Complete member initialization
        }

        public Bookmark( ChromeBookmark sourceBookmark ) {
            Debug.Assert( sourceBookmark != null, "sourceBookmark != null" );
            sourceBookmark.assignTo( this);
        }


        public BookmarkRoots roots(){
            return roots_;
        }

        public void setRoots(BookmarkRoots value){
            roots_ = value;
        }


        public string serviceNameForOpen() {
            return serviceNameForOpen_;
        }

        public void setServiceNameForOpen(string value) {
            serviceNameForOpen_ = value;
        }

        public string uniqueIdentifier {
            get {
                String fParentPath = identifier();
                Bookmark itemParent = parent();
                while (itemParent != null)
                {
                    fParentPath = String.Concat(itemParent.identifier(), fParentPath);
                    itemParent = itemParent.parent();
                }
                return fParentPath;
            }
        }

        public void setParent( Bookmark parent ) {
            parent_ = parent;
        }

        public Bookmark parent() {
            return parent_;
        }

        public override void assignTo( ChromeBookmark item) {
            base.assignTo( item);

            var bookmark = (Bookmark) item;
            bookmark.setParent( parent_ );
            bookmark.setServiceNameForOpen( this.serviceNameForOpen() );
        }

        public override ChromeBookmark onChildCopy(ChromeBookmark child){
            var newChild = new Bookmark( child );
                newChild.setParent( this );
            return newChild;
        }

        public virtual IList<Bookmark> childrenToList() {
            var result = new List<Bookmark>();
            Debug.Assert( children != null, "children != null" );
            foreach ( ChromeBookmark child in children ) {
                var newChild = new Bookmark();
                child.assignTo( newChild);
                newChild.setParent( this );
                result.Add( newChild );
            }
            return result;
        }

        private Bookmark itemByIdentifier(IList<Bookmark>collection, String identifier) {
            Debug.Assert( collection != null, "collection != null" );
            return ( from item in collection 
                     let valid = ( item.identifier() == identifier ) 
                     where valid 
                     select item 
                     ).FirstOrDefault();
        }

        public IList<Bookmark> parentsForUniqueKey( String uniqueId) {
            var result = new List<Bookmark>();
            if ( uniqueId == "" ) {
                result.Add( this );
                return result;
            }

            IList<Bookmark> collection = new List<Bookmark> {this};

            const string pattern = "<([^>]*)>";
            var regex = new Regex( pattern, RegexOptions.IgnoreCase );
            Match regexMatch = regex.Match( uniqueId );
            while ( regexMatch.Success ) {

                Bookmark childById = itemByIdentifier(collection, regexMatch.Value);
                if (childById == null) {
                    break;
                }

                result.Add( childById );
                collection = childById.childrenToList();
                regexMatch = regexMatch.NextMatch();
            }

            return result;
        }

        public IList<Bookmark> childrenForUniqueKey( String uniqueId) {
            if ( uniqueId == "" ) {
                return childrenToList();
            }

            Bookmark itemThatFound = this;
            IList<Bookmark> collection = new List<Bookmark> {this};

            const string pattern = "<([^>]*)>";
            var regex = new Regex( pattern, RegexOptions.IgnoreCase );
            Match regexMatch = regex.Match( uniqueId );
            while ( regexMatch.Success ) {

                Bookmark childById = itemByIdentifier(collection, regexMatch.Value);

                if ( childById == null ) {
                    itemThatFound = this;
                    break;
                }
                
                itemThatFound = childById;
                collection = itemThatFound.childrenToList();

                regexMatch = regexMatch.NextMatch();
            }

            return itemThatFound.childrenToList();
        }


    }
}